﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using OnLineExamModel;
using System.Collections.Generic;
using OnLineExamBLL;

public partial class Web_SingleSelectManage : System.Web.UI.Page
{
    SingleSelectedManager bll = new SingleSelectedManager();
    protected void Page_Load(object sender, EventArgs e)
    {
        this.Page.Title = "单选题管理";
        if (!IsPostBack)
        {

            string userId = User.Identity.Name;
            var loginuser = new UserManager().GetModel(userId);
            string userName = loginuser.UserName;
            Label i1 = (Label)Page.Master.FindControl("labUser");
            i1.Text = userName;


            string depid = loginuser.DepID+"";

            #region  绑定部门 科目下拉菜单
            this.ddlCourse.DataTextField = "CourseName";
            this.ddlCourse.DataValueField = "CourseID";
            this.ddlCourse.DataSource = new OnLineExamBLL.Course().GetList("depid=" + depid);
            this.ddlCourse.DataBind();
            this.ddlCourse.Items.Insert(0, new ListItem("请选择科目", ""));


            this.ddlDepinfo.DataTextField = "Name";
            this.ddlDepinfo.DataValueField = "ID";
            this.ddlDepinfo.DataSource = new OnLineExamBLL.DepInfo().GetAllList();
            this.ddlDepinfo.DataBind();
            this.ddlDepinfo.Items.Insert(0, new ListItem("请选择", ""));

            this.ddlDepinfo.Items.FindByValue(depid).Selected = true;

            this.ddlDepinfo.Enabled = false;

            if (User.IsInRole("管理员"))
            {
                this.ddlDepinfo.Enabled = true;
            }
            #endregion

            var lists = bll.GetList("depid=" + depid);
            this.GridView1.DataSource = lists;
            this.GridView1.DataBind();


        }
    }

    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        //string alert = @"window.onload=function(){confirm('确认要删除吗?');}";
        //Page.ClientScript.RegisterStartupScript(this.GetType(), "hhh", alert, true);

        SingleProblem mp = (SingleProblem)GridView1.Rows[e.RowIndex].DataItem;

        string id = GridView1.DataKeys[e.RowIndex].Value.ToString();


        string depid = Session["depid"].ToString();
        SingleSelectedManager.Delete(id);
        var lists = bll.GetList("depid="+depid);
        this.GridView1.DataSource = lists;
        this.GridView1.DataBind();
    }
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow || e.Row.RowType == DataControlRowType.Separator)
        {
            //Label label1 = e.Row.FindControl("Label1") as Label;
            //label1.Text = (e.Row.RowIndex + 1).ToString();
        }
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#cbe2fa'");
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='#FFFFFF'");
        }
    }
    protected void ddlCourse_SelectedIndexChanged(object sender, EventArgs e)
    {
        this.GridView1.DataSourceID = null;
        string selectvaule = this.ddlCourse.SelectedValue;
        string depid = Session["depid"].ToString();
        this.GridView1.DataSource = bll.GetList("depid=" + depid + "and courseid=" + ddlCourse.SelectedValue);
        this.GridView1.DataBind();
    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        int pageindex = e.NewPageIndex ;
        if (this.ddlCourse.SelectedValue != "")
        {
            string depid = Session["depid"].ToString();
            string sqlwhere = "depid=" + depid + "and courseid=" + ddlCourse.SelectedValue;
            this.GridView1.DataSource = bll.GetList("depid=" + depid + "and courseid=" + ddlCourse.SelectedValue); //bll.GetListByPage("depid=" + depid + "and courseid=" + ddlCourse.SelectedValue, "", this.GridView1.PageSize, this.GridView1.PageSize * pageindex);

            this.GridView1.PageIndex = pageindex; 
            this.GridView1.DataBind();
        }
        else
        {
            string depid = Session["depid"].ToString();
            this.GridView1.DataSource = bll.GetList(" depid=" + depid );//bll.GetListByPage("depid=" + depid + "", "", this.GridView1.PageSize, this.GridView1.PageSize * pageindex);
           
            this.GridView1.PageIndex = pageindex;
            this.GridView1.DataBind();
        }
    }
}
